package com.zone.vehicle.superapp.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.zone.vehicle.lib.view.VerticalTabLayout;
import com.zone.vehicle.superapp.databinding.ActivityMainBinding;
import com.zone.vehicle.superapp.utils.LoginUtil;
import com.zone.vehicle.superapp.view.adapter.AppFragmentPagerAdapter;
import com.zone.vehicle.superapp.view.adapter.TabAdapter;
import com.zone.vehicle.superapp.view.appmanager.AppManagerFragment;
import com.zone.vehicle.superapp.view.common.base.BaseActivity;
import com.zone.vehicle.superapp.view.home.HomeFragment;
import com.zone.vehicle.superapp.view.login.LoginActivity;
import com.zone.vehicle.superapp.view.login.secret.LoginSecretFragment;
import com.zone.vehicle.superapp.view.service.SupperService;
import com.zone.vehicle.superapp.view.setting.SettingActivity;
import com.zone.vehicle.superapp.view.setting.SuggestionFragment;
import java.util.ArrayList;

import static com.zone.vehicle.lib.view.VerticalTabLayout.TAB_MODE_FIXED;

public class WelcomeActivity extends BaseActivity {

    private static final String TAG = WelcomeActivity.class.getSimpleName();

    private static final String HOME_FRAGMENT = "HomeFragment";

    private ActivityMainBinding binding;
    private TabAdapter tabAdapter;
    private HomeFragment homeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startService(new Intent(this, SupperService.class));
        initViewPager();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (LoginUtil.isLogin()){
            binding.tabLayout.setupWithViewPager(binding.viewPager, new TabAdapter(this, binding.tabLayout, LoginUtil.getUserInfo()));
        }else {
            binding.tabLayout.setupWithViewPager(binding.viewPager, tabAdapter);
        }
        Log.d(TAG, "resume");
    }

    @Override
    protected void setContentView() {
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
    }

    private void initViewPager(){
        binding.viewPager.setAdapter(new AppFragmentPagerAdapter(getSupportFragmentManager(), new ArrayList<>(getPagesMap().values())));
        binding.tabLayout.addOnTabSelectedListener(new MyOnTabSelectedListener());
        tabAdapter = new TabAdapter(this, binding.tabLayout);
        binding.tabLayout.setupWithViewPager(binding.viewPager, tabAdapter);
        binding.tabLayout.setTabMode(TAB_MODE_FIXED);

        binding.tabLayout.getTabAt(0).select();
    }

    private void goLogin(){
        startActivity(new Intent(this, LoginActivity.class));
    }

    private void goSetting(){
        startActivity(new Intent(this, SettingActivity.class));
    }

    @Override
    public void initFragmentPages() {
        homeFragment = new HomeFragment();
        addPage(homeFragment);
        addPage(new LoginSecretFragment());
        addPage(new AppManagerFragment());
        addPage(new SuggestionFragment());//随便填一个占位
    }
    private class MyOnTabSelectedListener implements VerticalTabLayout.OnTabSelectedListener{

        @Override
        public boolean beforeSelect(VerticalTabLayout.VerticalTab newTab, VerticalTabLayout.VerticalTab currentTab) {
            if (newTab.getPosition() == 3) {
                if (LoginUtil.isLogin()) {
                    goSetting();
                } else {
                    goLogin();
                }
                return false;
            } else if (newTab.getPosition() == 1){
                return false;
            }
            Log.d(TAG, "tabLayout beforeSelect "+newTab.getText());
            return true;
        }

        @Override
        public void onTabSelected(VerticalTabLayout.VerticalTab tab) {
            Log.d(TAG, "tabLayout onTabSelected ");
        }

        @Override
        public void onTabUnselected(VerticalTabLayout.VerticalTab tab) {
            Log.d(TAG, "tabLayout onTabUnselected ");
        }

        @Override
        public void onTabReselected(VerticalTabLayout.VerticalTab tab) {
            Log.d(TAG, "onTabReselected="+tab.getPosition());
            if (tab.getPosition() == 3) {
                homeFragment.reset();
            }
        }
    }



    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "pause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}